package com.wonderful.eyepetizer.ui.pages.detail

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Create
import androidx.compose.material.icons.outlined.Favorite
import androidx.compose.material.icons.outlined.Share
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.zIndex
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.app.ktx.toPx
import com.wonderful.eyepetizer.data.entity.bean.ItemData
import com.wonderful.eyepetizer.ui.pages.detail.viewmodel.VideoDetailViewModel
import com.wonderful.eyepetizer.ui.theme.Black_54
import com.wonderful.eyepetizer.utils.DateUtils
import com.wonderful.eyepetizer.utils.ScreenUtils
import dagger.hilt.android.AndroidEntryPoint

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/1/30 11:04
 */
@AndroidEntryPoint
class VideoDetailActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = android.graphics.Color.BLACK
        WindowInsetsControllerCompat(window, window.decorView).apply {
            isAppearanceLightStatusBars = false
        }
        val itemData = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra(KEY_ITEM_DATA, ItemData::class.java)
        } else {
            intent.getParcelableExtra(KEY_ITEM_DATA)
        }
        setContent {
            VideoDetailPage(itemData = itemData)
//            EyepetizerComposeTheme {
//                Surface(
//                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
//                ) {
//                    FullScreenScaffold {
//                        VideoDetailPage(itemData = itemData)
//                    }
//                }
//            }
        }

        // 代替 onBackPressed
//        onBackPressedDispatcher.addCallback(this,
//            object : OnBackPressedCallback(true) {
//                override fun handleOnBackPressed() {
//                    if (Jzvd.backPress()) {
//                        return
//                    }
//                }
//            })
    }

    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }

    companion object {
        const val KEY_ITEM_DATA = "key_item_data"
        const val VIDEO_SMALL_CARD_TYPE = "videoSmallCard"
        fun actionStart(context: Context, itemData: ItemData) {
            val intent = Intent(context, VideoDetailActivity::class.java)
            intent.putExtra(KEY_ITEM_DATA, itemData)
            context.startActivity(intent)
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
internal fun VideoDetailPage(itemData: ItemData?) {
    val context = LocalContext.current
    val viewModel: VideoDetailViewModel = viewModel()
    val pullRefreshState =
        rememberPullRefreshState(refreshing = viewModel.refreshing.value, onRefresh = {
            itemData?.run {
                viewModel.getRelateVideoList(this.id)
            }
        })

    itemData?.run {
        LaunchedEffect(key1 = Unit) {
            viewModel.getRelateVideoList(itemData.id)
            viewModel.saveVideo(itemData)
        }
    }

    Box(modifier = Modifier.fillMaxSize()) {
        itemData?.run {
            Image(
                painter = rememberAsyncImagePainter(model = "${itemData.cover.blurred}/thumbnail/${ScreenUtils.getScreenHeight()}x${ScreenUtils.getScreenWidth()}"),
                contentDescription = null,
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.FillHeight
            )

            Column(modifier = Modifier.fillMaxSize()) {
                AndroidView(factory = {
                    val jzvdStd = JzvdStd(context)
                    jzvdStd.layoutParams =
                        ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200.toPx())
                    jzvdStd
                }) {
                    it.setUp(itemData.playUrl, itemData.title)
                    it.startVideoAfterPreloading()
                }

                Box(
                    modifier = Modifier
                        .pullRefresh(pullRefreshState)
                        .zIndex(-1f)
                ) {
                    LazyColumn {
                        item { VideoInfoItemWidget(itemData = itemData) }
                        items(viewModel.itemList) {
                            if (it.type == VideoDetailActivity.VIDEO_SMALL_CARD_TYPE) {
                                VideoRelateItemWidget(itemData = it.data)
                            } else {
                                Text(
                                    text = it.data?.text ?: "",
                                    color = Color.White,
                                    fontSize = 16.sp,
                                    fontWeight = FontWeight.Bold,
                                    modifier = Modifier.padding(10.dp)
                                )
                            }
                        }
                    }
                    PullRefreshIndicator(
                        refreshing = viewModel.refreshing.value,
                        state = pullRefreshState,
                        modifier = Modifier.align(
                            Alignment.TopCenter
                        )
                    )
                }
            }
        }
    }
}

@Composable
fun VideoInfoItemWidget(itemData: ItemData?) {
    itemData?.run {
        Column(modifier = Modifier.fillMaxWidth()) {
            Text(
                text = itemData.title,
                color = Color.White,
                fontSize = 18.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(start = 10.dp, top = 10.dp)
            )
            Text(
                text = "${itemData.category} / ${DateUtils.formatDateMsByYMDHM(itemData.author?.latestReleaseTime ?: 0)}",
                color = Color.White,
                fontSize = 12.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(start = 10.dp, top = 10.dp)
            )
            Text(
                text = itemData.description,
                color = Color.White,
                fontSize = 14.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(start = 10.dp, top = 10.dp, end = 8.dp)
            )
            Row(
                modifier = Modifier.padding(start = 10.dp, top = 10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    imageVector = Icons.Outlined.Favorite,
                    contentDescription = null,
                    modifier = Modifier.size(14.dp),
                    tint = Color.White
                )
                Text(
                    text = "${itemData.consumption.collectionCount}",
                    color = Color.White,
                    fontSize = 14.sp,
                    modifier = Modifier.padding(start = 3.dp)
                )
                Icon(
                    imageVector = Icons.Outlined.Share,
                    contentDescription = null,
                    modifier = Modifier
                        .padding(start = 30.dp)
                        .size(14.dp),
                    tint = Color.White
                )
                Text(
                    text = "${itemData.consumption.shareCount}",
                    color = Color.White,
                    fontSize = 14.sp,
                    modifier = Modifier.padding(start = 3.dp)
                )
                Icon(
                    imageVector = Icons.Outlined.Create,
                    contentDescription = null,
                    modifier = Modifier
                        .padding(start = 30.dp)
                        .size(14.dp),
                    tint = Color.White
                )
                Text(
                    text = "${itemData.consumption.replyCount}",
                    color = Color.White,
                    fontSize = 14.sp,
                    modifier = Modifier.padding(start = 3.dp)
                )
            }
            Divider(
                thickness = 0.5.dp, color = Color.White, modifier = Modifier.padding(top = 10.dp)
            )
            Row(
                modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = rememberAsyncImagePainter(model = itemData.author?.icon),
                    contentDescription = null,
                    modifier = Modifier
                        .padding(10.dp)
                        .size(40.dp)
                        .clip(
                            CircleShape
                        ),
                    contentScale = ContentScale.Crop
                )
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .padding(vertical = 10.dp)
                ) {
                    Text(
                        text = itemData.author?.name ?: "",
                        color = Color.White,
                        fontSize = 15.sp,
                        modifier = Modifier.padding(top = 3.dp)
                    )
                    Text(
                        text = itemData.author?.description ?: "",
                        color = Color.White,
                        fontSize = 13.sp,
                        modifier = Modifier.padding(top = 3.dp)
                    )
                }
                Text(
                    text = stringResource(id = R.string.add_follow),
                    color = Color.Gray,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .clip(
                            RoundedCornerShape(5.dp)
                        )
                        .background(Color.White)
                        .padding(horizontal = 10.dp, vertical = 5.dp)
                )
            }
            Divider(thickness = 0.5.dp, color = Color.White)
        }
    }
}

@Composable
fun VideoRelateItemWidget(itemData: ItemData?) {
    val context = LocalContext.current
    itemData?.run {
        Row(modifier = Modifier
            .padding(start = 10.dp, top = 10.dp, end = 10.dp, bottom = 5.dp)
            .clickable {
                VideoDetailActivity.actionStart(context, itemData)
            }) {
            Box {
                Image(
                    painter = rememberAsyncImagePainter(model = itemData.cover.detail),
                    contentDescription = null,
                    modifier = Modifier
                        .width(135.dp)
                        .height(80.dp)
                        .clip(
                            RoundedCornerShape(5.dp)
                        ),
                    contentScale = ContentScale.Crop
                )
                Text(
                    text = DateUtils.formatDateMsByMS(itemData.duration.toLong() * 1000),
                    color = Color.White,
                    fontWeight = FontWeight.Bold,
                    fontSize = 10.sp,
                    modifier = Modifier
                        .padding(end = 5.dp, bottom = 5.dp)
                        .clip(RoundedCornerShape(5.dp))
                        .background(Black_54)
                        .padding(3.dp)
                        .align(Alignment.BottomEnd)
                )
            }
            Column(
                modifier = Modifier
                    .padding(10.dp)
                    .weight(1f)
            ) {
                Text(
                    text = itemData.title,
                    color = Color.White,
                    fontWeight = FontWeight.Bold,
                    fontSize = 14.sp
                )
                Text(
                    text = "#${itemData.category} / ${itemData.author?.name}",
                    color = Color.White,
                    fontSize = 12.sp,
                    modifier = Modifier.padding(top = 15.dp)
                )
            }
        }
    }
}

